package com.fy.springmvc2.controller;


import com.fy.springmvc2.entity.User;
import com.fy.springmvc2.service.intf.UserService;
import com.fy.springmvc2.vo.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/vue")
public class VueUserController {

    //分页-查询全部
    @Autowired
    private UserService service;
    @RequestMapping("/users")
    public R<PageInfo<User>> getAllUser(@RequestParam(value = "pageIndex",required = false,defaultValue = "1") Integer pageIndex,
                                        @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

//        System.out.println(".......");
        PageInfo<User> userPageInfo = service.selectPage(pageIndex, pageSize);
//        System.out.println(userPageInfo);
        return R.ok(userPageInfo);

    }

    //查询全部
    @RequestMapping("/test")
    public R<List<User>> queryUserAll(){
//        System.out.println("请求成功。。。");
        List<User> users = service.selectAll();
//        System.out.println(users);
        return R.ok(0, "成功", users);
    }


    //通过id删除数据 测试vue前端请求url（ url:`http://localhost:9090/vue/del/${row.id}`）
    @RequestMapping("/del/{id}")
    public R deleteById(@PathVariable("id") Integer id){
        service.delete(id);
        return R.ok();
    }

    //通过id删除数据 测试vue前端请求url （ url:"http://localhost:9090/vue/del1?id="+row.id ）
    @RequestMapping("/del1")
    public R deleteById1(Integer id){
        service.delete(id);
        return R.ok();
    }

    //根据ID查询单个用户
    @GetMapping("/queryById/{id}")
    public R<User> queryById(@PathVariable("id") Integer id){

        return R.ok(service.selectById(id));
    }

    //修改数据
    @RequestMapping("/update")
    public R<Integer> queryById(User user){

        return R.ok(service.update(user));
    }

}
